/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.view;

import java.awt.Component;
import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import org.apache.log4j.Logger;
import org.dss.gil.service.InitializationService;
import org.dss.gil.service.MessagesService;
import org.dss.gil.util.Globals;
import org.dss.gil.util.StringFunctions;


/**
 *  
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */
public class Configurations extends javax.swing.JFrame {
    
    private static Logger logger = Logger.getLogger(Configurations.class.getName());
    private static Configurations instance;
    
    public static Configurations getInstance(){
        if(instance==null){
            instance = new Configurations();
        }
        return instance;
    }
    
    private Configurations() {
        initComponents();
        setLabels();
        setLocationRelativeTo(null);
        setIconImage(Globals.logoImg);
    }

    private void setLabels(){
        setTitle(Globals.language.getMessage("title.config"));
        jTabbedPane1.setTitleAt(0,Globals.language.getMessage("title.tab.conf"));
        jLab_Language.setText(Globals.language.getMessage("label.conf.language"));
        
        //Load languages list
        StringFunctions stf = new StringFunctions();
        String[] languages = stf.getTokens(Globals.config.getProperty("language.list"), ",");
        jCom_Languages.setModel(new DefaultComboBoxModel(languages));
        jCom_Languages.setSelectedItem(Globals.config.getProperty("language.actual.desc"));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jCom_Languages = new javax.swing.JComboBox();
        jLab_Language = new javax.swing.JLabel();
        jBut_save = new javax.swing.JButton();

        setResizable(false);

        jLab_Language.setText("Language");

        jBut_save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/save_x24.png"))); // NOI18N
        jBut_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBut_save)
                    .addComponent(jLab_Language)
                    .addComponent(jCom_Languages, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLab_Language)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCom_Languages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBut_save)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("General", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBut_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_saveActionPerformed
        try {
            String oldLanguage = Globals.config.getProperty("language.actual.desc");
            String language = jCom_Languages.getSelectedItem().toString().trim();
            Globals.config.setProperty("language.actual", language.substring(0, 2).toLowerCase());
            Globals.config.setProperty("language.actual.desc", language);
            Globals.config.setProperty("tabs.max", "25");
            Globals.config.setProperty("pool.undo.size", "25");
            Globals.config.save(Globals.USER_CONFIG_PATH, Globals.CONFIGURATIONS_INFO);
            if(!language.equals(oldLanguage)){
                reloadLanguage();
            }
            MessagesService msg_message = MessagesService.getInstance();
            msg_message.showInformationMessage(this, Globals.language.getMessage("message.file.saved") , null);
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }//GEN-LAST:event_jBut_saveActionPerformed

    private void reloadLanguage(){
        InitializationService init = InitializationService.getInstance();
        init.loadLanguageBundle();
        if(Globals.aboutView!=null){Globals.aboutView.setLabels();}
        if(Globals.calcView!=null){Globals.calcView.setLabels();}
        if(Globals.confView!=null){Globals.confView.setLabels();}
        if(Globals.frequenciesView!=null){Globals.frequenciesView.setLabels();}
        if(Globals.ideView!=null){
            Globals.ideView.setLabels();
            for(Component c : Globals.ideView.getTabPane().getComponents()){
                if(c instanceof TablePanel){
                    TablePanel t = (TablePanel) c;
                    t.setLabels();
                    t.setColumnsSize();
                }
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBut_save;
    private javax.swing.JComboBox jCom_Languages;
    private javax.swing.JLabel jLab_Language;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}
